from pyecharts.charts import Pie
import pandas as pd
from pyecharts import options as opts
import numpy as np

df = pd.read_excel('描述性统计.xlsx', sheet_name = 'Sheet1')

def pieAdd(title, x, y):
    pie = Pie()
    y=np.array(y)
    k=np.sum(y)/100
    y=y/k
    y=np.around(y,2)
    pie.add("", [list(z) for z in zip(x, y)]).set_global_opts(title_opts=opts.TitleOpts(title=title, pos_left='40%'),
                                                              legend_opts=opts.LegendOpts(pos_bottom = '0%')).set_series_opts(
            label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    return pie

def drawPie(colName, exclude=[]):
    classDict={}
    for index, row in df.iterrows():
        val=row[colName]
        val=str(val)
        if val in exclude:
            continue
        if not val in classDict.keys():
            classDict[val]=1
        else:
            classDict[val]+=1

    pieAdd(colName, classDict.keys(), list(classDict.values())).render('pie.html')
    input(colName+' ready next')

def drawMultiPie(colNameList, title, labelList):
    valList=[]
    for i in range(len(labelList)):
        valList.append(0)

    for index, row in df.iterrows():
        i=0
        for colName in colNameList:
            if row[colName]==1:
                valList[i]+=1
            i+=1

    pieAdd(title, labelList, valList).render('pie.html')
    input(title+' ready next')

def genColNameList(question, num):
    ret=[]
    for i in range(1,num+1):
        ret.append(question+'|'+str(i))
    return ret

for index, _ in df.iteritems():
    drawPie(index)